﻿using Comm100.Framework.Domain.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using Bot.Domain.Bo;
using Castle.Windsor;
using Bot.Domain.Specifications;

namespace Bot.Domain.Categories.Service
{
    public class QuickReplyItemDomainService : IQuickReplyItemDomainService
    {
        private IWindsorContainer _container;
        private readonly IRepository<Guid, QuickReplyItem> _repository;

        public QuickReplyItemDomainService(IRepository<Guid, QuickReplyItem> repository, IWindsorContainer container)
        {
            this._repository = repository;
            this._container = container;
        }

        public QuickReplyItem Create(QuickReplyItem quickReplyItem)
        {
            if (quickReplyItem.Id == Guid.Empty)
            {
                quickReplyItem.Id = Guid.NewGuid();
            }
            return _repository.Create(quickReplyItem);
        }

        public QuickReplyItem Get(Guid id)
        {
            return _repository.Get(id);
        }

        public QuickReplyItem Update(QuickReplyItemUpdateBo bo)
        {
            QuickReplyItem quickReplyItem = _repository.Get(bo.Id);
            quickReplyItem.Id = bo.Id;
            //quickReplyItem.QuickReplyId = bo.QuickReplyId;
            quickReplyItem.Type = bo.Type;
            quickReplyItem.Text = bo.Text;
            quickReplyItem.IntentId = bo.IntentId;
            quickReplyItem.Order = bo.Order;

            _repository.Update(quickReplyItem);

            return quickReplyItem;

        }

        public IEnumerable<QuickReplyItem> List()
        {
            return _repository.ListAll();
        }

        public void Delete(Guid id)
        {
            QuickReplyItem quickReplyItem = _repository.Get(id);
            _repository.Delete(quickReplyItem);

        }
        public QuickReplyItem Get(Guid id, string include)
        {
            var result = _repository.Get(id);
            if (result != null && !string.IsNullOrEmpty(include) && include.Split(',').Contains("intent"))
            {
                if (result.Type == Enum.EnumQuickReplyType.goToIntent && result.IntentId.HasValue && result.IntentId != Guid.Empty)
                {
                    IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                    result.Intent = intentDomainService.Get(result.IntentId.Value, include: "");
                }                
            }
            return result;
        }
        public IEnumerable<QuickReplyItem> GetByQuickReplyId(Guid quickReplyId, string include = "")
        {
            var list = _repository.List(new QuickReplyItemFilterSpecification(quickReplyId)).ToList();
            if (list != null && list.Count > 0 && !string.IsNullOrEmpty(include)&& include.Split(',').Contains("intent"))
            {
                IIntentDomainService intentDomainService = _container.Resolve<IIntentDomainService>();
                foreach (var item in list)
                {
                    if(item.Type == Enum.EnumQuickReplyType.goToIntent && item.IntentId.HasValue && item.IntentId != Guid.Empty)
                    {
                        item.Intent = intentDomainService.Get(item.IntentId.Value, include: "");
                    }
                }
            }
            return list;
        }
    }
}
